package com.sugar.real.estate.module.business.service.info;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.module.business.controller.admin.apartment.vo.ApartmentUpdateReqVO;
import com.sugar.real.estate.module.business.controller.admin.info.vo.InfoAllRespVO;
import com.sugar.real.estate.module.business.controller.admin.info.vo.InfoPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.info.vo.InfoRespVO;
import com.sugar.real.estate.module.business.controller.admin.info.vo.InfoSaveReqVO;
import com.sugar.real.estate.module.business.controller.app.info.vo.AppInfoPageReqVO;
import com.sugar.real.estate.module.business.controller.app.info.vo.AppInfoRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.info.InfoDO;
import com.sugar.real.estate.module.business.dal.mysql.info.InfoMapper;
import com.sugar.real.estate.module.business.dal.mysql.infohouse.InfoHouseMapper;
import com.sugar.real.estate.module.business.util.I18nUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.sugar.real.estate.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.real.estate.module.business.enums.ErrorCodeConstants.INFO_NOT_EXISTS;

/**
 * 资讯信息 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;
    @Resource
    private InfoHouseMapper infoHouseMapper;

    @Override
    public Long createInfo(InfoSaveReqVO createReqVO) {
        // 插入
        InfoDO info = BeanUtils.toBean(createReqVO, InfoDO.class);
        infoMapper.insert(info);
        // 返回
        return info.getId();
    }

    @Override
    public void updateInfo(InfoSaveReqVO updateReqVO) {
        // 校验存在
        validateInfoExists(updateReqVO.getId());
        // 更新
        InfoDO updateObj = BeanUtils.toBean(updateReqVO, InfoDO.class);
        infoMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfo(Long id) {
        // 校验存在
        validateInfoExists(id);
        // 删除
        infoMapper.deleteById(id);
    }

    private void validateInfoExists(Long id) {
        if (infoMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public InfoDO getInfo(Long id) {
        return infoMapper.selectById(id);
    }

    @Override
    public PageResult<InfoRespVO> getInfoPage(InfoPageReqVO pageReqVO) {
        return infoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InfoAllRespVO> getInfos() {
        List<InfoDO> infos = infoMapper.selectList();
        return BeanUtils.toBean(infos, InfoAllRespVO.class, o -> o.setTitle(o.getTitleZhCn() + "(" + o.getTitleEnUs() + ")"));
    }

    @Override
    public void updateStatus(ApartmentUpdateReqVO updateReqVO) {
        infoMapper.update(InfoDO::getId, updateReqVO.getId(), InfoDO::getIsHot, updateReqVO.getIsShow());
    }

    // ======================================== APP 接口 ========================================

    @Override
    public AppInfoRespVO appGetInfo(Long id) {
        InfoDO infoDO = infoMapper.selectById(id);

        // 若资讯不为空，则资讯浏览次数 +1
        if (Objects.nonNull(infoDO))
            infoMapper.update(InfoDO::getId, id, InfoDO::getViewCount, infoDO.getViewCount() + 1);

        return BeanUtils.toBean(infoDO, AppInfoRespVO.class, o ->
                o.setTitle(I18nUtil.getMessage(o, "getTitle"))
                        .setContent(I18nUtil.getMessage(o, "getContent"))
                        .setAuthor(I18nUtil.getMessage(o, "getAuthor")));
    }

    @Override
    public PageResult<AppInfoRespVO> appGetInfoPage(AppInfoPageReqVO pageReqVO) {
        Long houseId = pageReqVO.getHouseId();

        // 若楼盘ID为空，则直接查询资讯表
        if (Objects.isNull(houseId)) {
            return BeanUtils.toBean(infoMapper.appSelectInfos(pageReqVO), AppInfoRespVO.class,
                    o -> o.setTitle(I18nUtil.getMessage(o, "getTitle"))
                            .setContent(I18nUtil.getMessage(o, "getContent"))
                            .setAuthor(I18nUtil.getMessage(o, "getAuthor")));
        } else {
            Integer type = pageReqVO.getType();
            if (Objects.isNull(type))
                return new PageResult<>(Collections.emptyList(), 0L);

            // 根据楼盘信息查询楼盘资讯
            PageResult<AppInfoRespVO> pageResult = infoHouseMapper.selectPageByHouseId(pageReqVO);
            for (AppInfoRespVO respVO : pageResult.getList()) {
                respVO.setTitle(I18nUtil.getMessage(respVO, "getTitle"))
                        .setContent(I18nUtil.getMessage(respVO, "getContent"))
                        .setAuthor(I18nUtil.getMessage(respVO, "getAuthor"));
            }
            return pageResult;
        }
    }

    @Override
    public List<AppInfoRespVO> getHotList() {
        List<InfoDO> infos = infoMapper.selectList(Wrappers.<InfoDO>lambdaQuery().eq(InfoDO::getIsHot, 1));
        return BeanUtils.toBean(infos, AppInfoRespVO.class,
                o -> o.setTitle(I18nUtil.getMessage(o, "getTitle"))
                        .setContent(I18nUtil.getMessage(o, "getContent"))
                        .setAuthor(I18nUtil.getMessage(o, "getAuthor")));
    }

}